# -*- encoding:utf-8 -*-
# @time: 2023/4/15 16:47
# @author: Maxs_hu
import pymysql
import settings


class Connect(object):
    def __init__(self):  # 连接操作
        self.conn = conn = pymysql.connect(**settings.MYSQL_CONN_PARAMS)
        self.cursor = conn.cursor(pymysql.cursors.DictCursor)

    def __enter__(self):
        return self  # 返回当前对象

    def __exit__(self, exc_type, exc_val, exc_tb):  # 关闭操作
        self.cursor.close()
        self.conn.close()

    def exec(self, sql, **kwargs):
        self.cursor.execute(sql, kwargs)
        self.conn.commit()

    def fetchone(self, sql, **kwargs):
        self.cursor.execute(sql, kwargs)
        result = self.cursor.fetchone()
        return result

    def fetchall(self, sql, **kwargs):
        self.cursor.execute(sql, kwargs)
        result = self.cursor.fetchall()
        return result
